﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using QMERP.Domain.Dtos;

namespace QMERP.WebApi.Attributes
{
    /// <summary>
    /// 返回值过滤器
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class ResponseWrapperAttribute: ResultFilterAttribute
    {
        /// <summary>
        /// 统一返回值
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is FileStreamResult || context.Result is FileContentResult)
            {
                //base.OnResultExecuting(context);
            }
            else
            {
                ApiResponse response = new ApiResponse();
                if (context.Result != null)
                {
                    var objectResult = context.Result as ObjectResult;
                    response.Result = objectResult?.Value;
                }
                context.Result = new ObjectResult(response);
            }
        }
    }
}
